/*
 * Created on May 6, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.common.http;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import javax.servlet.http.*;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;

import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.HttpRequestConverter;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;
import org.vectrics.user.PersonSearchCriteria;

/**
 * @author mmoore
 */
public class HttpUtils {
	public static void logRequestInfo(HttpServletRequest servletRequest, Logger logger, Level level) {
		Request request = HttpRequestConverter.convertRequest(servletRequest);
	}
	
	public static void logRequestInfo(Request request, Logger logger, Level level) {
		/*
		Principal userPrincipal = request.getUserPrincipal();

		logger.log(level, "======================================================================");
		if (userPrincipal != null) {
			logger.log(level, "[UserInfo] - principal = " + userPrincipal.getName());
		} else {
			logger.log(level, "[UserInfo] - User principal not found");
		}
		*/
		
		try {
			Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(request);
			if (person == null) {
				logger.log(level, "No authenticated user");
			}
			else {
				logger.log(level, "Authenticated user = " + person.getName());
			}
		} catch (Exception ex) {
			logger.log(level, "Exception finding authenticated user.");
		}
		
		//logger.info("Request.pathInfo = " + request.getPathInfo());
		logger.log(level, "Request.requestUri = " + request.getRequestURI());
		logger.log(level, "Request.queryString = " + request.getQueryString());
		//logger.log(level, "Request.pathTranslated = " + request.getPathTranslated());
		
		
		logger.log(level, "______________________________________________________________________");
		logger.log(level, "[Session Info] - Session Id = " + request.getSessionId());
		Set<String> sessionAttrNames = request.getSessionAttributeNames();
		Iterator<String> iter = sessionAttrNames.iterator();
		while (iter.hasNext()) {
			String name = iter.next();
			Object obj = request.getSessionAttribute(name);
			if (obj != null) {
				
				if (obj.getClass() == String.class) {
					logger.log(level, "[Session Info] - Attribute: " + name + ", \"" + (String)obj + "\"");
				}
				else {
					logger.log(level, "[Session Info] - Attribute: " + name + ", " + obj.getClass().getName());
				}
				
				if (obj.getClass() == PersonSearchCriteria.class) {
					logger.log(level, "   UserSearchCriteria = " + ((PersonSearchCriteria)obj).toString());
				}
			}
		} 
		
		logger.log(level, "______________________________________________________________________");
		Iterator<String> paramNames = request.getAttributeNames().iterator();
		if (paramNames.hasNext()) {
			while (paramNames.hasNext()) {
				String paramName = (String)paramNames.next();
				String paramValue = request.getParameter(paramName);
				logger.log(level, "[Request] - attribute: " + paramName + ", " + paramValue);
			}
		}
		else {
			logger.log(level, "[ NO REQUEST ATTRIBUTES ]");
		}
		
		logger.log(level, "______________________________________________________________________");
		paramNames = request.getParameterNames().iterator();
		if (paramNames.hasNext()) {
			while (paramNames.hasNext()) {
				String paramName = (String)paramNames.next();
				String paramValue = request.getParameter(paramName);
				logger.log(level, "[Request] - parameter: " + paramName + ", " + paramValue);
			}
		}
		else {
			logger.log(level, "[ NO REQUEST PARAMETERS ]");
		}
		
		logger.log(level, "======================================================================");
	}
	
	public static String encodeHttpQueryValue(String before) {
		StringBuffer after = new StringBuffer();
		for (int i = 0; i < before.length(); i++) {
			char c = before.charAt(i);
			if (c == ' ') {
				after.append("+");
			} else if (c == '&') {
				after.append("%26");
			} else if (c == '+') {
				after.append("%2B");
			} else if (c == '=') {
				after.append("%3D");
			} else if (c == '?') {
				after.append("%3F");
			} else if (c == '<') {
				after.append("%3C");
			} else if (c == '>') {
				after.append("%3E");
			} else if (c == ':') {
				after.append("%3A");
			} else if (c == ';') {
				after.append("%3B");
			} else if (c == '#') {
				after.append("%23");
			} else if (c == '(') {
				after.append("%28");
			} else if (c == ')') {
				after.append("%29");
			} else if (c == '{') {
				after.append("%7B");
			} else if (c == '}') {
				after.append("%7D");
			} else if (c == '|') {
				after.append("%7C");
			} else if (c == '[') {
				after.append("%5B");
			} else if (c == ']') {
				after.append("%5E");
			} else if (c == '!') {
				after.append("%21");

			} else if (c == '~') {
				after.append("%7E");
				
			} else if (c == ',') {
				after.append("%2C");
								
				
			} else if (c == '\\') {
				after.append("%5C");
				
			} else if (c == '`') {
				after.append("%60");
				
			} else if (c == '\'') {
				after.append("%27");
				
			} else {
				after.append(c);
			}
		}
		return(after.toString());
	}
}
